package com.system.borrow.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.system.borrow.MyApplication;
import com.system.borrow.R;
import com.system.borrow.bean.Action;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2016/11/30 17:05
 * 描    述：消息适配器
 * ================================================
 */
public class MsgAdapter extends BaseAdapter{
    public Context context;
    public LayoutInflater inflater;
    private int type = 0;
    private Handler mHandler = new Handler();
    List<Action> list = new ArrayList<>();
    public MsgAdapter(Context context,List<Action> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<Action> list) {
        this.list = list;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if(view==null){
            view = inflater.inflate(R.layout.item_msg,null);
            viewHolder = new ViewHolder();
            viewHolder.tv_state = (TextView) view.findViewById(R.id.tv_state);
            viewHolder.tv_name = (TextView) view.findViewById(R.id.tv_name);
            viewHolder.tv_time = (TextView) view.findViewById(R.id.tv_time);
            viewHolder.iv_img = (ImageView) view.findViewById(R.id.iv_img);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        final Action action = list.get(i);
        MyApplication.imageLoader.displayImage(action.img,viewHolder.iv_img,MyApplication.long_options);
        if(type==0){
            viewHolder.tv_state.setVisibility(View.GONE);
        }else{
            viewHolder.tv_state.setVisibility(View.VISIBLE);
            if(action.statusText!=null){
                if(action.statusText.equals("进行中")){
                    viewHolder.tv_state.setTextColor(context.getResources().getColor(R.color.status));
                }else{
                    viewHolder.tv_state.setTextColor(context.getResources().getColor(R.color.grey_status));
                }
                viewHolder.tv_state.setText(action.statusText);
            }
            viewHolder.tv_time.setText("活动时间:"+action.startTime+"——"+action.endTime);
        }
        if(action.title.contains(" ")){
            String[] split = action.title.split(" ");
            SpannableStringBuilder spannable = new SpannableStringBuilder(
                    action.title);// 用于可变字符串
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            spannable.setSpan(span, 0, split[0].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            viewHolder.tv_name.setText(spannable);
        }else{
            viewHolder.tv_name.setText(action.title);
        }
        return view;
    }
    public class ViewHolder{
        TextView tv_state,tv_name,tv_time;
        ImageView iv_img;
    }
}
